#region Copyright (C) 2007 Jens Winter

//Copyright (C) 2007 Jens Winter

//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.

//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.

//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

#endregion

using System;
using System.Collections.Generic;
using System.Data;
using Flynn.Core.Activities;
using Flynn.Core.ActivityResults;
using MbUnit.Framework;
using Rhino.Mocks;

namespace Flynn.Core.Tests.Activities
{
    [TestFixture]
    public class ExecuteSqlStatementsActivityFixture
    {
        private MockRepository _mocks;

        private IExecuteSqlStatementActivity _executeSqlStatementActivity;
        private IExecuteSqlStatementsActivity _executeSqlStatementsActivity;

        [SetUp]
        public void SetUp()
        {
            _mocks = new MockRepository();

            _executeSqlStatementActivity = _mocks.CreateMock<IExecuteSqlStatementActivity>();
            _executeSqlStatementsActivity = new ExecuteSqlStatementsActivity(_executeSqlStatementActivity);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ExecuteSqlStatementActivity_Must_Not_Be_Null()
        {
            new ExecuteSqlStatementsActivity(null);
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Executing_SqlStatements_SqlStatements_Must_Not_Be_Null()
        {
            _executeSqlStatementsActivity.ExecuteSqlStatements(null, _mocks.Stub<IDbConnection>());
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void When_Executing_SqlStatements_Connection_Must_Not_Be_Null()
        {
            _executeSqlStatementsActivity.ExecuteSqlStatements(_mocks.Stub<IList<string>>(), null);
        }

        [Test]
        public void Can_Execute_SqlStatements()
        {
            IList<string> sqlStatements = new List<string> { "SqlStatement1", "SqlStatement2", "SqlStatement3" };
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                using (_mocks.Ordered())
                {
                    Expect.Call(_executeSqlStatementActivity.ExecuteSqlStatement("SqlStatement1", connection)).Return(ExecuteSqlStatementResult.Success());
                    Expect.Call(_executeSqlStatementActivity.ExecuteSqlStatement("SqlStatement2", connection)).Return(ExecuteSqlStatementResult.Success());
                    Expect.Call(_executeSqlStatementActivity.ExecuteSqlStatement("SqlStatement3", connection)).Return(ExecuteSqlStatementResult.Success());
                }
            }

            using (_mocks.Playback())
            {
                ExecuteSqlStatementsResult result = _executeSqlStatementsActivity.ExecuteSqlStatements(sqlStatements, connection);

                Assert.IsFalse(result.Failed);
            }
        }

        [Test]
        public void Executing_SqlStatements_Fails_If_Executing_A_Single_Statement_Fails()
        {
            IList<string> sqlStatements = new List<string> { "SqlStatement1", "SqlStatement2", "SqlStatement3" };
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                using (_mocks.Ordered())
                {
                    Expect.Call(_executeSqlStatementActivity.ExecuteSqlStatement("SqlStatement1", connection)).Return(ExecuteSqlStatementResult.Failure("Failure message"));
                }
            }

            using (_mocks.Playback())
            {
                ExecuteSqlStatementsResult result = _executeSqlStatementsActivity.ExecuteSqlStatements(sqlStatements, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }

        [Test]
        public void Executing_SqlStatements_Fails_If_Executing_A_Single_Statement_Throws_Exception()
        {
            IList<string> sqlStatements = new List<string> { "SqlStatement1", "SqlStatement2", "SqlStatement3" };
            IDbConnection connection = _mocks.Stub<IDbConnection>();

            using (_mocks.Record())
            {
                using (_mocks.Ordered())
                {
                    Expect.Call(_executeSqlStatementActivity.ExecuteSqlStatement("SqlStatement1", connection)).Throw(new Exception("Failure message"));
                }
            }

            using (_mocks.Playback())
            {
                ExecuteSqlStatementsResult result = _executeSqlStatementsActivity.ExecuteSqlStatements(sqlStatements, connection);

                Assert.IsTrue(result.Failed);
                Assert.AreEqual("Failure message", result.FailureMessage);
            }
        }
    }
}